package com.rongxing.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.rongxing.entity.User;
import com.rongxing.mapper.UserMapper;
import com.rongxing.service.UserService;
import com.rongxing.model.R;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.rongxing.entity.table.UserTableDef.USER;

/**
 * User 表数据服务层接口实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public JsonObject findUser(JsonObject param) {
        LOGGER.debug("进入服务");
        String name = StringUtil.trimOrNull(param.getString("name"));
        String age = StringUtil.trimOrNull(param.getString("age"));
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.where(USER.NAME.like(name).when(StringUtil::isNotBlank))
                .and(USER.AGE.eq(age).when(StringUtil::isNotBlank));

        List<User> list = list(queryWrapper);
        return new JsonObject(R.ok(list).toString());
    }
}